package edu.chl.dat255.bluebanana.presentation.impl;

import edu.chl.dat255.bluebanana.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import edu.chl.dat255.bluebanana.data.impl.DbConst;
import edu.chl.dat255.bluebanana.presentation.ICommunicationProfileSettings;
import edu.chl.dat255.bluebanana.utils.DatabaseUtil;

public class CommunicationProfileSettings extends Activity implements
		ICommunicationProfileSettings {
	private CheckBox blockCallsButton;
	private CheckBox autoRespondButton;
	private CheckBox flightModeButton;
	private CheckBox wifiButton;
	private CheckBox bluetoothButton;
	private CheckBox packetDataButton;
	private EditText autoRespondText;
	private LinearLayout focusLayout;
	private long profileId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.communicationsettings);

		profileId = getIntent().getLongExtra("Profile", -1);

		autoRespondText = (EditText) findViewById(R.id.autoRespondMessage);

		blockCallsButton = (CheckBox) findViewById(R.id.blockCallsButton);
		autoRespondButton = (CheckBox) findViewById(R.id.autoRespondButton);
		flightModeButton = (CheckBox) findViewById(R.id.flightModeButton);
		wifiButton = (CheckBox) findViewById(R.id.wifiButton);
		bluetoothButton = (CheckBox) findViewById(R.id.blueToothButton);
		packetDataButton = (CheckBox) findViewById(R.id.packetDataButton);
		
		autoRespondButton.setOnCheckedChangeListener(this);

		focusLayout = (LinearLayout) findViewById(R.id.focusLayout);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		blockCallsButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_BLOCKED_CALLS, this));
		autoRespondButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_AUTO_MESSAGE_ENABLE, this));
		flightModeButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_FLIGHT_MODE_ENABLE, this));
		wifiButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_WIFI_ENABLE, this));
		bluetoothButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_BLUETOOTH_ENABLE, this));
		packetDataButton.setChecked(DatabaseUtil.getBooleanValue(profileId, DbConst.KEY_PACKET_DATA_ENABLE, this));
		autoRespondText.setText(DatabaseUtil.getTextValue(profileId, DbConst.KEY_AUTO_MESSAGE_CONTENT, this));
		autoRespondText.setEnabled(autoRespondButton.isChecked());
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (buttonView.equals(autoRespondButton)) {
			autoRespondText.setEnabled(isChecked);
			if (!isChecked)
				focusLayout.requestFocus();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		DatabaseUtil.sendValueToDatabase(profileId,
				DbConst.KEY_AUTO_MESSAGE_CONTENT, autoRespondText.getText().toString(),
				CommunicationProfileSettings.this);

		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_BLOCKED_CALLS, blockCallsButton.isChecked(), this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_AUTO_MESSAGE_ENABLE, autoRespondButton.isChecked(), this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_FLIGHT_MODE_ENABLE, flightModeButton.isChecked(), this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_WIFI_ENABLE, wifiButton.isChecked(), this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_BLUETOOTH_ENABLE, bluetoothButton.isChecked(), this);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_PACKET_DATA_ENABLE, packetDataButton.isChecked(), this);
	}
}
